﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;

namespace HZ.Supervise.Web.Enterprise
{
    public partial class ComResonseAmountDetail_DZ : BasePage
    {
        IBLL_Company ic = HZ.Supervise.BLLFactory.IBLL_Company();
        IBLL_Area ia = BLLFactory.IBLL_Area();
        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj;
        string mess = "";
        public ComResonseAmountDetail_DZ()
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                BindData();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_CompanyPS_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager_CompanyPS.CurrentPageIndex = 1; 
            BindData(); 
        }

        /// <summary>
        /// 绑定数据
        /// modify  by张雄  2011.5.4
        /// </summary>
        public void BindData()
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            int t = 0;
            vs.Flag = "1";
            //System.Data.DataTable dt = ic.GetTotalDetail_DZByComResponse(vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);
            System.Data.DataTable dt = ic.GetTotalDetail_DZ(vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);
            AspNetPager_CompanyPS.RecordCount = t;

            this.gvwPSLInfo.PageSize = AspNetPager_CompanyPS.PageSize;
            this.gvwPSLInfo.DataSource = dt;
            this.gvwPSLInfo.DataBind();

            vs.Flag = "1.5";
            dt = ic.GetTotalDetail_DZ(vs, 1, 0, out t);

            double sumReturnCount = 0;
            double sumReturnAmount = 0;

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                sumReturnCount += Convert.ToDouble(dt.Rows[j]["PurCount"]);
                sumReturnAmount += Convert.ToDouble(dt.Rows[j]["PurAmount"]);
            } 
            lblMemo.Text = string.Format("响应数量：{0}个；响应金额：{1}元；", sumReturnCount.ToString(), sumReturnAmount.ToString("0.00"));
            //医院
            if (!string.IsNullOrEmpty(vs.HospitalID))
            {
                mess = "医疗机构[" + vs.HospitalName + "]";
            }
            //配送企业
            if (!string.IsNullOrEmpty(vs.CompanyID_PS))
            {
                mess = "配送企业[" + vs.CompanyName_PS + "]";
            }
            //投标企业
            if (!string.IsNullOrEmpty(vs.CompanyID_TB))
            {
                mess = "生产企业[" + vs.CompanyName_TB + "]";
            }
            //地区
            if (!string.IsNullOrEmpty(ddlArea.SelectedAreaID))
            {
                mess = "地区编号[" + ddlArea.SelectedAreaID + "]";
            }
            //流水号
            if (!string.IsNullOrEmpty(vs.ProcureCatalogID))
            {
                mess = "流水号[" + vs.ProcureCatalogID + "]";
            } 
            AddLog(string.Format("查询{0}低值响应汇总统计数据!", mess));
             
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager_CompanyPS.CurrentPageIndex = 1;
        }
        /// <summary>
        /// 分页    县区
        /// by张雄
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void CompanyPSL_PS_PageChanged(object sender, EventArgs e)
        {
            BindData(); 
        }
         
        /// <summary>
        /// 导出
        /// modify  by  张雄    2011.5.5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = Queryby();
            int t = 0;
            vs.Flag = "1";
            System.Data.DataTable dt = ic.GetTotalDetail_DZ(vs, 1, 0, out t);//ic.GetTotalDetail_DZByComResponse(vs, 1, 0, out t);
            //医院
            if (!string.IsNullOrEmpty(vs.HospitalID))
            {
                mess = "医疗机构[" + vs.HospitalName + "]";
            }
            //配送企业
            if (!string.IsNullOrEmpty(vs.CompanyID_PS))
            {
                mess = "配送企业[" + vs.CompanyName_PS + "]";
            }
            //投标企业
            if (!string.IsNullOrEmpty(vs.CompanyID_TB))
            {
                mess = "生产企业[" + vs.CompanyName_TB + "]";
            }
            //地区
            if (!string.IsNullOrEmpty(ddlArea.SelectedAreaID))
            {
                mess = "地区编号[" + ddlArea.SelectedAreaID + "]";
            }
            //流水号
            if (!string.IsNullOrEmpty(vs.ProcureCatalogID))
            {
                mess = "流水号[" + vs.ProcureCatalogID + "]";
            }
            AddLog(string.Format("导出{0}低值响应汇总统计数据成功!", mess));
            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }



        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery Queryby()
        {
            SuperviseQuery vs = new SuperviseQuery();
            vs = (SuperviseQuery)Session[Request.QueryString["sesid"]];
            divtip.Visible = false;
            //医院名称
            if (!string.IsNullOrEmpty(vs.HospitalID))
            {
                txtHospitalName.Text = vs.HospitalName;
                txtHospitalName.ReadOnly = true;
            } 
            //配送企业
            if (!string.IsNullOrEmpty(vs.CompanyID_PS))
            {
                txtCompanyName_PS.Text = vs.CompanyName_PS;
                txtCompanyName_PS.ReadOnly = true;
            }
            //投标企业
            if (!string.IsNullOrEmpty(vs.CompanyID_TB))
            {
                txtProduceCompany_SC.Text = vs.CompanyName_TB;
                txtProduceCompany_SC.ReadOnly = true;
            }
            //地区
            if (!string.IsNullOrEmpty(vs.AreaName))
            {
                ddlArea.InitAreaID = vs.AreaID; 
            }
            //流水号
            if (!string.IsNullOrEmpty(vs.ProcureCatalogID))
            {
                txtGoodsID.Text = vs.ProcureCatalogID;
                txtGoodsID.ReadOnly = true;
            }
            switch (vs.BackPage.ToLower())
            {
                case "hospital":
                    divtip.Visible = true;
                    lblAreaInfo.Text = "当前查询医院为：" + vs.HospitalName;
                    break;
                case "comps":
                    divtip.Visible = true;
                    lblAreaInfo.Text = "当前查询配送企业为：" + vs.CompanyName_PS;
                    break;
                case "comsc":
                    divtip.Visible = true;
                    lblAreaInfo.Text = "当前查询投标企业为：" + vs.CompanyName_TB;
                    break;
                case "area":
                    divtip.Visible = true;
                    lblAreaInfo.Text = "当前查询地区：" + vs.AreaName;
                    vs.AreaName = null;
                    break;
                case "goods":
                    divtip.Visible = true;
                    lblAreaInfo.Text = "当前查询产品流水号为：" + vs.ProcureCatalogID;
                    break;
            } 
            vs.AreaID = ddlArea.SelectedAreaID;
            vs.SortFields = " ProcureCatalogID";// OrderFileds.SelectedValue + " " + OrderType.SelectedValue; 
            vs.HospitalName = txtHospitalName.Text.Trim(); 
            vs.CompanyName_PS = txtCompanyName_PS.Text.Trim(); 
            vs.CompanyName_TB = HZ.Supervise.DAL.DAL_Helper.Replace(txtProduceCompany_SC.Text.Trim());
            vs.Outlookc = HZ.Supervise.DAL.DAL_Helper.Replace(txtOutLook.Text.Trim());
            vs.Unit = HZ.Supervise.DAL.DAL_Helper.Replace(txtUnit.Text.Trim());
            vs.ProcureCatalogID = HZ.Supervise.DAL.DAL_Helper.Replace(txtGoodsID.Text.Trim());
            vs.RegName = HZ.Supervise.DAL.DAL_Helper.Replace(txtRegName.Text.Trim());
            vs.Brand = HZ.Supervise.DAL.DAL_Helper.Replace(txtBrand.Text.Trim());
            vs.ProductName = HZ.Supervise.DAL.DAL_Helper.Replace(txtProductName.Text.Trim());
            vs.GoodsName = HZ.Supervise.DAL.DAL_Helper.Replace(tGoodsName.Text.Trim()); 
            return vs;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            string url = Request.QueryString["returnUrl"];

            Response.Redirect(url);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        } 
    }
}